//
//  VisitorView.swift
//  HJWeibo
//
//  Created by huangjiong on 16/1/31.
//  Copyright © 2016年 huangjiong. All rights reserved.
//

import UIKit

//定义一个协议,处理按钮点击时,通知外部
protocol VisitorViewDelegate: NSObjectProtocol
{
    //注册回调
    func loginButtonDidClicked()
    //注册回调
    func registerButtonDidClicked()
}

class VisitorView: UIView {
    
    
    //定义一代理属性
    weak var delegate:VisitorViewDelegate?

    //重写构造方法
    override init(frame: CGRect) { 
        
        super.init(frame: frame)
        
        //重写此方法的目的,是为了对子控件进行添加和布局
        //1.添加
        addSubview(iconView)
        addSubview(maskBgView)
        addSubview(homeIcon)
        addSubview(messageLabel)
        addSubview(loginButton)
        addSubview(registerButton)
        
        //布局
        // 2.1设置背景
        iconView.xmg_AlignInner(type: XMG_AlignType.Center, referView: self, size: nil)
        // 2.2设置小房子
        homeIcon.xmg_AlignInner(type: XMG_AlignType.Center, referView: self, size: nil)
        // 2.3设置文本
        messageLabel.xmg_AlignVertical(type: XMG_AlignType.BottomCenter, referView: iconView, size:nil)
        
        // "哪个控件" 的 "什么属性" "等于" "另外一个控件" 的 "什么属性" 乘以 "多少" 加上 "多少"
        let widthCons = NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 224)
        addConstraint(widthCons)
        
        // 2.4设置按钮
        registerButton.xmg_AlignVertical(type: XMG_AlignType.BottomLeft, referView: messageLabel, size: CGSize(width: 100, height: 30), offset: CGPoint(x: 0, y: 20))
        
        loginButton.xmg_AlignVertical(type: XMG_AlignType.BottomRight, referView: messageLabel, size: CGSize(width: 100, height: 30), offset: CGPoint(x: 0, y: 20))
        
        // 2.5设置蒙版
        maskBgView.xmg_Fill(self)

    }
    
    //注意:重写UIView的构造方法之后,必须再重写这个方法
    required init?(coder aDecoder: NSCoder) {
        
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 外部控制
    func setupVisitorInfo(isHome:Bool, imageName:String, message:String)
    {
        //修改中间图标
        homeIcon.image = UIImage(named: imageName)
        //修改文本
        messageLabel.text = message
        
        //判断是否为首页
        //如果是首页,就让转盘进行动画
        if isHome == true
        {
            startAnimation()
            
        }
        //不是首页时,就让转盘隐藏
        else
        {
            iconView.hidden = true
        }
    }
    
    //MARK: - 内部方法,转运转盘
    private func startAnimation()
    {
        //创建一个基本动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        //设置属性
        anim.toValue = 2 * M_PI
        anim.duration = 20
        anim.repeatCount = MAXFLOAT
        anim.removedOnCompletion = false
        //将动画添加到图层上
        iconView.layer.addAnimation(anim, forKey: nil)
    }
    
    //MARK: - 按钮点击事件
    func loginButtonClick()
    {
        //回调通知外部
        delegate?.loginButtonDidClicked()
    }
    
    func registerButtonClick()
    {
        delegate?.registerButtonDidClicked()
    }
    
    //MARK: - 懒加载子控件
    //转盘
    private lazy var iconView: UIImageView = {
       let imgView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        return imgView
    }()
    
    //房子图标
    private lazy var homeIcon: UIImageView = {
       let imgView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        return imgView
    }()
    
    //文本标签
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        label.numberOfLines = 0
        label.textColor = UIColor.darkGrayColor()
        label.text = "我人有手和一地"
        return label
    }()
    
    //登录按钮
    private lazy var loginButton: UIButton = {
        let btn = UIButton()
        btn.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        //添加事件
        btn.addTarget(self, action: "loginButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    
    //注册按钮
    private lazy var registerButton: UIButton = {
       let btn = UIButton()
        btn.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        //添加事件
        btn.addTarget(self , action: "registerButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    
    //用来挡住一半转盘的一个view
    private lazy var maskBgView: UIImageView = {
        let imgView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        
        return imgView
    }()

}
